
# A2_plot_market_shares
#===============================================================================

# Description: This file plots average market shares for each exporter and creates 
# one of the Appendix Figure A1 subplots

rm(list = ls())

setwd("D:/data_replication")

library("data.table")  
library("ggplot2")  
library("ggrepel")  
library("stargazer")  

output <- fread('statistics_and_graphs/market_shares/output_files/market_shares_exporter.txt')
output[ , V1 := gsub("s", "", V1)]
output <- output[V1 != ""]
output <- output[V1 != "_con"]
setnames(output, "V1", "partner")
output[ , partner := as.numeric(partner)]
setnames(output, "V2", "share")
output[ , share := as.numeric(share)]

gdp_data <- fread('data/gdp_per_capita/gdp_per_capita_defl.csv')
gdp_data <- gdp_data[year == 2007]

output <- merge(output, gdp_data, by = "partner")
output <- as.data.table(output)

iso3 <- fread('data/country_codes/ISO3_partner_codes.csv') 
output <- merge(output, iso3, by = "partner")
output <- as.data.table(output)

output[ , gdp_log := log(gdp_per_capita_defl)]

output <- output[share != 0]

plot_shares <- ggplot(output, aes(x=gdp_log, y=share)) +
               geom_text(label=output$ISO3, size = 5) +
               theme(text = element_text(size=17)) +
               geom_smooth(method=lm) +
               labs(x = "Exporter GDP per capita (in logs)", y = "Market share (logs)") +
               theme(axis.title=element_text(size=30)) +
               theme(axis.text=element_text(size=18))

ggsave("statistics_and_graphs/market_shares/plot_shares.png", plot = plot_shares, width = 12, height = 9)


